<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //创建一个person类
      class Person {
        //构造器方法
        constructor(name, age) {
          //构造器的this指向实例化对象
          this.name = name;
          this.age = age;
        }

        //一般方法
        speak() {
          //speak方法放在了类的原型对象上,供实例使用，
          //通过Person实例调用speack时，speak中的this指向实例化对象
          console.log(`我叫${this.name},我今年${this.age}岁了`);
        }
      }

      //创建一个Student类，继承person类
      class Student extends Person {
        constructor(name, age, grade) {
          super(name, age);
          this.grade = grade;
        }

        speak() {
          //重写从父类继承过来的方法
          console.log(
            `我叫${this.name},我今年${this.age}岁了,我的年级是${this.grade}`
          );
        }

        study() {
          console.log("我很努力的学习");
        }
      }

      //创建实例对象
      const p1 = new Person("tom", 18);
      const p2 = new Person("jerry", 19);
      const s1 = new Student("小李", 20, "高一");

      console.log(p1);
      console.log(p2);
      console.log(s1);

      p1.speak();
      p2.speak();
      s1.speak();
      s1.study();
    </script>
  </body>
</html>
